package y23.Sep;
//https://leetcode.cn/problems/truncate-sentence/
//力扣1816 截断句子
public class Test3 {
    public static void main(String[] args) {
        Solution1 solution = new Solution1();
        System.out.println(solution.truncateSentence("Hello how are you Contestant", 4));
        System.out.println(solution.truncateSentence("What is the solution to this problem", 4));
        System.out.println(solution.truncateSentence("chopper is not a tanuki", 5));
    }
}
class Solution1 {
    public String truncateSentence(String s, int k) {
        int n = s.length();
        int i = 1;
        while(i <= n){
            if (i == n || s.charAt(i) == ' ') {
                k--;
                if (k == 0){
                    break;
                }
            }
            i++;
        }
        return s.substring(0, i);
    }
}
